import {
  colorFetch,
  typeFetch,
  seriesFetch,
  fetchTbox,
  brandFetch,
  fetchProject,
  searchSeries,
  searchSeriesList,
  searchTypeList,
  searchColorList,
  findFactory,
  findDealerByFactory,
  seriesLabels,
  fetchDealerByLogin,
  fetchTemplate,
  findVehicleActiveList,
  seriesByBrandCode,
  findVehicleProfile,
  findDicData,
  findVehicleBatchActiveList,
  findVehicleActiveListByDealer,
  brandSeriesLabels,
  findSeriesByids,
  findSeriesByProjects,
  findFactoryBrand,
  findDealerByBrand,
  findRedisData,
  findProjectIdsByBrandCode,
  findDealerByBrandCode,
  findProjectByUser,
} from '../services/basedata';

export default {
  namespace: 'basedata',
  state: {
    dic:{},
    series: {
      list: [],
      pagination: {},
    },
    seriesReport: [],
    type: {
      list: [],
      pagination: {},
    },
    color: {
      list: [],
      pagination: {},
    },
    data: {
      list: [],
      pagination: {},
    },
    dealer:{
      list:[],
      pagination:{}
    },
    seriesLabels: [],
    brandSeriesLabels: [],
    brand: [],
    seriesMultipBrands:[],
    loading: true,
    redisData:{},
  },

  effects: {
    * clearData({}, {put}) {
      yield put({
        type: 'save',
        payload: {loading: true},
      });
      yield put({
        type: 'save',
        payload:
          {
            series: {pagination: {total: 0}},
            type: {pagination: {total: 0}},
            loading: false
          }
      });
    },
    *dicDataFetch({ callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findDicData);
      yield put({
        type: 'dicData',
        payload: response.dic ,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *redisDataFetch({ callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findRedisData);
      yield put({
        type: 'saveRedisData',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *brandFetch({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(brandFetch, payload);
      yield put({
        type: 'brand',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *colorFetch({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(colorFetch, payload);
      yield put({
        type: 'color',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *typeFetch({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(typeFetch, payload);
      yield put({
        type: 'type',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *seriesFetch({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(seriesFetch, payload);
      yield put({
        type: 'series',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *seriesByBrandCode({ payload,callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      const response = yield call(seriesByBrandCode, payload);
      yield put({
        type: 'save',
        payload: { seriesReport: response, loading: false },
      });
      if (callback) callback(response);
    },
    *seriesByBrandCodes({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findSeriesByids, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *projectByBrandFetch({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(projectByBrandFetch, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchtbox({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchTbox, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVehicleProfile({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleProfile, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchProject({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchProject, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchSeries({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchSeries, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *typeList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchTypeList, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *seriesList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchSeriesList, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *colorList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchColorList, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *seriesLabels({ callback, payload }, { call, put }) {
      const response = yield call(seriesLabels, payload);
      yield put({
        type: 'saveLabels',
        payload: response,
      });
      if (callback) callback(response);
    },
    *brandSeriesLabels({ callback, payload }, { call, put }) {
      const response = yield call(brandSeriesLabels, payload);
      yield put({
        type: 'saveBrandLabels',
        payload: response,
      });
      if (callback) callback(response);
    },
    *factoryList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findFactory, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *dealerList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findDealerByFactory, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchDealerByLogin({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchDealerByLogin, payload);
      yield put({
        type:'dealer',
        payload:response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchTemplate({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchTemplate, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVehicleActiveList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleActiveList, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVehicleBatchActiveList({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleBatchActiveList, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVehicleActiveListByDealer({ callback, payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleActiveListByDealer, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },

    *findSeriesByProjects({ callback, payload }, { call, put }) {
      const response = yield call(findSeriesByProjects, payload);
      if (callback) callback(response);
    },
    *findFactoryBrand({ callback, payload }, { call, put }) {
      const response = yield call(findFactoryBrand, payload);
      if (callback) callback(response);
    },
    *findDealerByBrand({ callback, payload }, { call, put }) {
      const response = yield call(findDealerByBrand, payload);
      if (callback) callback(response);
    },
    *findProjectIdsByBrandCode({ callback, payload }, { call, put }) {
      const response = yield call(findProjectIdsByBrandCode, payload);
      if (callback) callback(response);
    },
    *findProjectIdByBrandCode({ callback, payload }, { call, put }) {
      const response = yield call(findProjectByUser, payload);
      if (callback) callback(response);
    },
    *findDealerByBrandCode({ callback, payload }, { call, put }) {
      const response = yield call(findDealerByBrandCode, payload);
      if (callback) callback(response);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    dealer(state, action) {
      return {
        ...state,
        dealer: action.payload,
      };
    },
    brand(state, action) {
      return {
        ...state,
        brand: action.payload,
      };
    },
    color(state, action) {
      return {
        ...state,
        color: action.payload,
      };
    },
    dicData(state, action) {
      return {
        ...state,
        dic: action.payload,
      };
    },
    saveRedisData(state, action) {
      return {
        ...state,
        redisData: action.payload,
      };
    },
    saveLabels(state, action) {
      return {
        ...state,
        seriesLabels: action.payload,
      };
    },
    saveBrandLabels(state, action) {
      return {
        ...state,
        brandSeriesLabels: action.payload,
      };
    },
    series(state, action) {
      return {
        ...state,
        series: action.payload,
      };
    },
    type(state, action) {
      return {
        ...state,
        type: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
